<?php


namespace app\api\controller;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class Sms
{


    function initialize()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }

    /**
     * 发送短信验证码
     * @param $phone
     * @param $param
     * @param string $TemplateCode
     * @return array|string
     */
    public function sendSmsCode($phone, $param, $TemplateCode = 'SMS_223025111')
    {

        $templateParam = json_encode($param, JSON_UNESCAPED_UNICODE);
        try {
            AlibabaCloud::accessKeyClient('LTAI5tMx6npUbah8DGiZ917t', 'ch8fcA9Eug9ECz1kFG4d6wVRV9n9f1')
                ->regionId('cn-hangzhou') // replace regionId as you need
                ->asDefaultClient();
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
//                 ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->connectTimeout(3600) // Throw an exception when Connection timeout
                ->timeout(3600) // Throw an exception when timeout
                ->options([
                    'query' => [
                        'PhoneNumbers' => $phone,
                        'SignName' => '沃优教育',
                        'TemplateCode' => $TemplateCode,
                        'TemplateParam' => $templateParam,
                        'RegionId' => 'cn-hangzhou',
                    ],
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        }
    }

}